#include <iostream>
using std::cout;
using std::endl;

class Point
{
public:
    static Point * getInstance(){
        if(nullptr == _pInstance){
            _pInstance = new Point(1,2);
        }
        return _pInstance;
    }

    void init(int x,int y){
        _ix = x;
        _iy = y;
    }

    static void destroy(){
        if(_pInstance){
            delete _pInstance;
            _pInstance = nullptr;
        }
    }

    void print(){
        cout << "(" << _ix
            << "," << _iy
            << ")" << endl;
    }
private:
    Point() = default;
    Point(int x,int y)
    : _ix(x)
    , _iy(y)
    {
        cout << "Point(int,int)" << endl;
    }

    ~Point(){
        cout << "~Point()" << endl;
    }

    Point(const Point & rhs) = delete;
    Point & operator=(const Point & rhs) = delete;
private:
    int _ix;
    int _iy;
    static Point * _pInstance;
};
Point * Point::_pInstance = nullptr;

#if 0
void test0(){
    Point * p1 = Point::getInstance();
    Point * p2 = Point::getInstance();
    cout << p1 << endl;
    cout << p2 << endl;
    p1->print();
    p2->print();
    /* p1->destroy(); */
    /* delete p1; */
    /* delete p2; */
}
#endif

void test1(){
    //单例模式的规范使用
    Point::getInstance()->print();
    //将初始化与赋值分开，想要使用不同的内容，
    //就先调用一次init函数
    Point::getInstance()->init(3,4);
    Point::getInstance()->print();
    Point::getInstance()->init(8,9);
    Point::getInstance()->print();
    //即使多次destroy也不会有double free问题
    Point::destroy();
    Point::destroy();
}

int main(void){
    test1();
    return 0;
}
